#include <pthread.h>
#include <string>
#include <ctime>
#include <functional>

// 利用面向对象的技术
class Thread
{
    static int thread_num;
    using fun = std::function<void*(void*)>;
private:
    void* thread_routine() { return _func(_args); }
    static void* thread_task(void* args)
    {
        Thread* pt = static_cast<Thread*>(args);
        auto ff = std::bind(&Thread::thread_routine, pt);
        ff();
        return nullptr;
    }
public:
    // 还需要保存一个回调函数来进行回调
    // 利用包装器
    Thread(const fun& c, void* args)
        :_func(c), _args(args), _name("thread-" + std::to_string(thread_num))
    {}

    void run() 
    { 
        _start_time_stamp = time(nullptr);
        pthread_create(&_tid, nullptr, thread_task, this);
        _is_running = true;
    }

    void join()
    {
        pthread_join(_tid, nullptr);
        _is_running = false;
    }

    std::string get_name() { return _name; }
    bool is_running() { return _is_running; }
    time_t get_start_time() { return _start_time_stamp; }

private:
    pthread_t _tid;
    bool _is_running = false;
    time_t _start_time_stamp;
    std::string _name;
    fun _func;
    void* _args;
};

int Thread::thread_num = 1;